
/*
 * Copyright 2014 Hieu Rocker
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.vanniktech.emoji;

import com.vanniktech.emoji.emoji.Emojicon;
import ohos.agp.components.ListContainer;
import ohos.agp.components.TableLayoutManager;
import ohos.agp.components.TextField;
import ohos.app.Context;

/**
 * EmojiconRecentsListContainer
 *
 * @author Daniele Ricci
 * @author Hani Al Momani (hani.momanii@gmail.com)
 */
public class EmojiconRecentsListContainer extends EmojiconListContainer implements EmojiconRecents {
    private final EmojiProvider mAdapter;
    private final EmojiconRecentsManager recents1;

    /**
     * 构造
     *
     * @param context        上下文
     * @param emojicons      数据
     * @param recents        接口
     * @param emojiconsPopup 软键盘
     * @param textField      输入框
     */
    public EmojiconRecentsListContainer(Context context, Emojicon[] emojicons,
                                        EmojiconRecents recents, EmojiconsPopup emojiconsPopup, EmojiconTextField... textField) {
        super(context, emojicons, recents, emojiconsPopup, textField);
        recents1 = EmojiconRecentsManager
                .getInstance(rootView.getContext());
        mAdapter = new EmojiProvider(rootView.getContext(), recents1);
        ListContainer gridView = (ListContainer) rootView.findComponentById(ResourceTable.Id_Emoji_GridView);
        TableLayoutManager tableLayoutManager = new TableLayoutManager();
        tableLayoutManager.setColumnCount(9);
        tableLayoutManager.setOrientation(ListContainer.HORIZONTAL);

        gridView.setLayoutManager(tableLayoutManager);
        gridView.setItemClickedListener((listContainer, component, i, l) -> {
            Emojicon emojicon = recents1.get(i);
            recents1.push(emojicon);
            refreshEmoji();
            for (TextField field : textField) {
                if (field.getTag().toString().equals("1")) {
                    String s = emojicon.getEmoji();
                    field.insert(s);
                    return;
                } else if (field.getTag().toString().equals("2")) {
                    String s = emojicon.getEmoji();
                    field.setText(s);
                    return;
                }
            }
        });
        gridView.setItemProvider(mAdapter);
        mAdapter.notifyDataChanged();
    }

    @Override
    public void addRecentEmoji(Context context, Emojicon emojicon, EmojiconTextField... textField) {
        recents1.push(emojicon);
        int count = mAdapter.getCount();
        for (int index = 0; index < count; index++) {
            mAdapter.notifyDataSetItemChanged(index);
        }
    }

    /**
     * 刷新表情
     */
    public void refreshEmoji() {
        int count = mAdapter.getCount();
        for (int index = 0; index < count; index++) {
            mAdapter.notifyDataSetItemChanged(index);
        }
    }
}